//
//  PKCommunityTopicTableViewCell.m
//  PianKe
//
//  Created by ybq on 15/12/30.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKCommunityTopicTableViewCell.h"

@implementation PKCommunityTopicTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _recommendImageView = [[UIImageView alloc] init];
    _recommendImageView.image = [UIImage imageNamed:@"jing"];
    [self.contentView addSubview:_recommendImageView];
    
    _title = [[UILabel alloc] init];
    _title.font = [UIFont systemFontOfSize:20];
    _title.numberOfLines = 0;
    [self.contentView addSubview:_title];
    
    _mainImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainImageView];
    
    _content = [[UILabel alloc] init];
    _content.textColor = [UIColor grayColor];
    _content.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_content];
    
    _addtime = [[UILabel alloc] init];
    _addtime.textColor = [UIColor lightGrayColor];
    _addtime.font = [UIFont systemFontOfSize:9];
    [self.contentView addSubview:_addtime];
    
    _comment = [[UIImageView alloc] init];
    UIImageView *commentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    commentImageView.image = [UIImage imageNamed:@"pinglun"];
    [_comment addSubview:commentImageView];
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 30, 15)];
    _commentLabel.textAlignment = NSTextAlignmentRight;
    _commentLabel.textColor = [UIColor lightGrayColor];
    _commentLabel.font = [UIFont systemFontOfSize:9];
    [_comment addSubview:_commentLabel];
    [self.contentView addSubview:_comment];
}

- (void)setModel:(PKCommunityTopicModel *)model {
    _model = model;
    _title.text = model.title;
    if (model.isrecommend == YES) {
        [_recommendImageView setFrame:CGRectMake(20, 25, 30, 30)];
        [_title setFrame:CGRectMake(52, 25, kWidth - 40, [self getLabelHeightByString:_title])];
    } else {
        [_recommendImageView setFrame:CGRectMake(20, 25, 0, 0)];
        [_title setFrame:CGRectMake(20, 25, kWidth - 40, [self getLabelHeightByString:_title])];
    }
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    _content.text = model.content;

    _addtime.text = model.addtime_f;
    _commentLabel.text = [NSString stringWithFormat:@"%@",model.comment];
    
    if (![model.coverimg isEqualToString:@""]) {
        [_mainImageView setFrame:CGRectMake(20, _title.frame.size.height + 45, 60, 60)];
        [_content setFrame:CGRectMake(_mainImageView.frame.origin.x + 70, _mainImageView.frame.origin.y + 10, kWidth - 110, 40)];
        _content.numberOfLines = 2;
        [_addtime setFrame:CGRectMake(20, _mainImageView.frame.origin.y + 75, 100, 10)];
    } else {
        [_mainImageView setFrame:CGRectMake(20, _title.frame.size.height + 45, 0, 0)];
        [_content setFrame:CGRectMake(20, _title.frame.size.height + 45, kWidth - 40, [self getLabelHeightByString:_content])];
        _content.numberOfLines = 3;
        [_addtime setFrame:CGRectMake(20, _content.frame.origin.y + _content.frame.size.height + 20, 100, 10)];
    }
    [_comment setFrame:CGRectMake(kWidth - 70, _addtime.frame.origin.y, 50, 15)];
}

- (CGFloat)getLabelHeightByString:(UILabel *)label {
    NSString *string = label.text;
    NSDictionary *attributes = @{NSFontAttributeName:label.font};
    CGSize size = {0, 0};
    
    if ([label isEqual:_title]) {
        if (_recommendImageView.frame.size.width == 0) {
            size = CGSizeMake(kWidth - 40, 1000);
        } else {
            size = CGSizeMake(kWidth - 70, 1000);
        }
        
    } else {
        if (_mainImageView.frame.size.width == 0) {
            size = CGSizeMake(kWidth - 40, 1000);
        } else {
            size = CGSizeMake(kWidth - 110, 60);
        }
        
    }

    CGSize contentactually = [string boundingRectWithSize:size options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:attributes context:nil].size;
    return contentactually.height;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
